<?php
/**
 * 管理员添加
 *
 * @author Dracowyn
 * @since 2023-11-14 10:37
 */

// 引入配置文件
include_once('config/config.php');
$loginAuth = check_auth();

if ($_POST) {
	// 接收数据
	$username = trim($_POST['username']);
	$password = trim($_POST['password']);
	$rePassword = trim($_POST['rePassword']);

	// 验证数据
	if (empty($username)) {
		showMsg('用户名不能为空');
		exit();
	}

	if (empty($password)) {
		showMsg('密码不能为空');
		exit();
	}

	if (empty($rePassword)) {
		showMsg('确认密码不能为空');
		exit();
	}

	if ($password != $rePassword) {
		showMsg('两次密码不一致');
		exit();
	}

	// 判断用户名是否存在
	$sql = "select username from pre_admin where username = '$username'";
	$findUser = find($sql);
	if ($findUser) {
		showMsg('用户名已存在');
		exit();
	}

	// 头像上传
	$res = upload('avatar');
	if ($res['code'] === 0) {
		showMsg($res['msg']);
	}

	// 密码加密
	$salt = randStr();
	$password = md5($password);
	$password = md5($password . $salt);

	// 添加数据
	$data = [
		'username' => $username,
		'password' => $password,
		'salt' => $salt,
		'avatar' => $res['data']['filename'] ?? '',
	];
	$result = insert('admin', $data);
	if ($result['code'] === 0) {
		if (isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
			@is_file('.' . $data['avatar']) && @unlink('.' . $data['avatar']);
		}
		showMsg($result['msg']);
		exit();
	} else {
		showMsg('添加成功', 'adminList.php');
	}
}
?>

<!DOCTYPE html>
<html lang="zh">
<head>
	<?php include_once('common/meta.php'); ?>
    <title>添加管理员</title>
</head>
<style>
    #upload {
        width: 200px;
        height: 200px;
        cursor: pointer;
    }
</style>

<body>

<?php include_once('common/header.php'); ?>

<?php include_once('common/menu.php'); ?>

<div class="content">
    <div class="header">
        <h1 class="page-title">添加管理员</h1>
    </div>
    <ul class="breadcrumb">
        <li><a href="index.php">首页</a> <span class="divider">/</span></li>
        <li><a href="adminList.php">管理员管理</a> <span class="divider">/</span></li>
        <li class="active">添加管理员</li>
    </ul>

    <div class="container-fluid">
        <div class="row-fluid">

            <div class="btn-toolbar">
                <button class="btn btn-primary" onClick="location='adminList.php'"><i class="icon-list"></i> 管理员列表
                </button>
                <div class="btn-group">
                </div>
            </div>

            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <form method="post" enctype="multipart/form-data">

                            <label for="username">用户名</label>
                            <input type="text" name="username" id="username" class="input-xxlarge"
                                   placeholder="请输入用户名">

                            <label for="password">密码</label>
                            <input type="password" name="password" id="password" class="input-xxlarge"
                                   placeholder="请输入密码">

                            <label for="rePassword">确认密码</label>
                            <input type="password" name="rePassword" id="rePassword" class="input-xxlarge"
                                   placeholder="请输入确认密码">

                            <label>头像</label>
                            <input type="file" name="avatar" id="avatar" class="input-xxlarge" hidden>
                            <img src="assets/images/upload.png" alt="" id="upload" onclick="avatar.click();">

                            <label></label>
                            <input class="btn btn-primary" type="submit" value="提交"/>

                        </form>
                    </div>
                </div>
            </div>

			<?php include_once('common/footer.php'); ?>

        </div>
    </div>
</div>
</body>
<?php include_once('common/script.php'); ?>
<script>
    $('#avatar').change(function () {
        let file = $(this)[0].files[0];

        if (file) {
            let reader = new FileReader();

            reader.readAsDataURL(file);

            reader.onload = function () {
                $('#upload').attr('src', reader.result);
            }
        }
    });
</script>
</html>
